import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
    info: {
      testpaper: {
        title: '',
        memo: '',
        imgs: []
      }
    },
    tipinfotit: '',
    tipinfo: '',
    _components_modal_hide: true,
  },
  parent() {
    $.parent().setData({
      page: 1,
      info: []
    })
  },
  inputhand(e) { //input的输入
    this.setData({
      'info.testpaper.title': e.detail.value
    })
  },
  texthand(e) { //文本域输入
    this.setData({
      'info.testpaper.memo': e.detail.value
    })
  },
  delete() { //删除的点击事件提示的模态框
    if (!this.check()) return
    this.setData({
      tipinfotit: '确定删除作业吗?',
      tipinfo: '删除后学员端无法查看该作业,已提交的作业会同步删除',
    })
    this.setData({
      _components_modal_hide: false
    })
  },
  againsend() { //重新发送的点击事件提示的模态框
    if (!this.check()) return
    this.setData({
      tipinfotit: '',
      tipinfo: '重新发布作业会覆盖之前发布的作业内容',
    })
    this.setData({
      _components_modal_hide: false
    })
  },
  alterimg() { //修改图片数据格式
    const {
      info
    } = this.data
    if (info.testpaper.imgs) {
      let a = info.testpaper.imgs.map((v, i) => { //保存的时候需要将 [{url:'............'},{}] 改成[1,2,3]
        return info.testpaper[i] = v.url
      });
      info.testpaper.imgs = a
    }
  },
  save() { //保存草稿
    const {
      info,
      group_uuid
    } = this.data
    if (!this.check()) return
    this.alterimg()
    $.post('/teacher/testpaper/create', {
      memo: info.testpaper.memo || '',
      title: info.testpaper.title || '',
      imgs: info.testpaper.imgs || [],
      group_uuid
    }, res => {
      this.parent()
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  send() { //发布
    const {
      group_uuid,
      info,
      testpaper_uuid
    } = this.data
    if (!this.check()) return
    this.alterimg()
    $.post('/teacher/testpaper/publish/assign', {
      group_uuid,
      imgs: info.testpaper.imgs || [],
      memo: info.testpaper.memo,
      testpaper_uuid: testpaper_uuid,
      title: info.testpaper.title,
    }, res => {
      this.parent()

      console.log('发布成功');
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  cancel() { //模态框的取消
    this.setData({
      _components_modal_hide: true
    })
  },
  determine() { //模态框的确定
    this.cancel()
    this.postinfo()
  },
  check() { //检查
    const {
      info
    } = this.data
    if (!info.testpaper) {
      $.tip('作业标题不能为空')
      return false
    } else if (info.testpaper && info.testpaper.title == '') {
      $.tip('作业标题不能为空')
      return false
    } else if (info.testpaper && info.testpaper.memo == '') {
      $.tip('作业内容不能为空')
      return false
    } else {
      return true
    }
  },
  postinfo() { //上传信息
    const {
      tipinfotit,
      info,
      group_uuid,
      testpaper_uuid
    } = this.data
    let obj = {
      testpaper_uuid
    }
    this.alterimg()
    if (tipinfotit == '') {
      //重新发布
      obj.imgs = info.testpaper.imgs,
      obj.memo = info.testpaper.memo,
      obj.title = info.testpaper.title,
      obj.group_uuid = group_uuid
      $.post('/teacher/testpaper/publish/assign', obj, res => {
        console.log('重新发布');
        this.parent()
        wx.navigateBack({
          delta: -1,
        })
      })
    } else if (tipinfotit == '确定删除作业吗?') {
      //删除
      $.get('/teacher/testpaper/remove', obj, res => {
        console.log('删除成功');
        this.parent()
        wx.navigateBack({
          delta: -1,
        })
      })
    }
  },
  afterRead(event) { //上传图片
    const {
      file
    } = event.detail;
    const {
      info
    } = this.data
    let files = file.map(v => { //因为vant 返回的解构是[{url:'............'}] $.uploadFileList 需要的结构式[1,2,3]
      return v.url
    })
    $.uploadFileList("/image/upload/oss", {
      prefix: "keday/",
      fileList: files
    }, res => {
      let a = res.map(i => { //返回的结构式[1,2,3,5] 需要改成vant需要的[{url:'............'},{}]
        return {
          url: i
        }
      })
      let arr //判断之前有没有 图片 有的话合并
      if (info.testpaper) {
        if (info.testpaper.imgs) {
          arr = [...info.testpaper.imgs, ...a]
        } else {
          arr = [...a]
        }
      } else {
        info.testpaper = {}
        arr = [...a]
        info.testpaper.imgs = arr
      }
      this.setData({
        'info.testpaper.imgs': arr
      })
    })
  },
  imgdelete(event) { //删除图片
    const {
      info
    } = this.data
    info.testpaper.imgs.splice(event.detail.index, 1)
    this.setData({
      info
    })
  },
  gojobdetails(e) { //去往工作详情
    const {
      studentpaper_uuid
    } = e.currentTarget.dataset
    if (studentpaper_uuid) {
      $.navigateTo(`/homework/jobdeails?studentpaper_uuid=${studentpaper_uuid}`)
    }
  },
  getinfo() {
    const {
      group_uuid,
      testpaper_uuid
    } = this.data
    $.get('/teacher/testpaper/detail?', {
      group_uuid,
      testpaper_uuid
    }, res => {
      this.setData({
        info: res
      })
      if (res.testpaper && res.testpaper.imgs) {
        let a = res.testpaper.imgs.map(i => {
          return {
            url: i
          }
        })
        this.setData({
          'info.testpaper.imgs': a
        })
      }
    })
  },
  onLoad(e) {
    const {
      group_uuid,
      testpaper_uuid,
      status
    } = e
    this.setData({
      group_uuid: group_uuid || '',
      testpaper_uuid: testpaper_uuid || '',
      status: status || ''
    })
  },
  onShow() {
    this.getinfo()
  }
})